<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('购物车列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>商品ID：</label>
                                <input type="text" name="cosId"/>
                            </li>
                            <li>
                                <label>商品名称：</label>
                                <input type="text" name="cosmetics.name"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-primary multiple disabled" onclick="submitAll()" shiro:hasPermission="admin:cart:edit">
                    <i class="fa fa-check"></i> 提交
                </a>
                <a class="btn btn-danger multiple disabled" onclick="submitAll()" shiro:hasPermission="admin:cart:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <h3 style="color: #00a65a">
                    总价: ￥<i id="totals">0</i>
                </h3>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('admin:cart:edit')}]];
        var removeFlag = [[${@permission.hasPermi('admin:cart:remove')}]];
        var prefix = ctx + "admin/cart";
        var totals = $('#totals');

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "购物车",
                onCheck: function(row,$element){
                    let rows = $.table.selectColumns('total');
                    let data = { "totals": rows.join() };
                    let totalPrice = 0;
                    let array = data.totals.split(',');
                    array.map(item => {
                        totalPrice += parseFloat(item);
                    });
                    totals.text(totalPrice.toFixed(2))
                },
                onUncheck: function(row,$element){
                    let rows = $.table.selectColumns('total');
                    let data = { "totals": rows.join() };
                    let totalPrice = 0;
                    let array = data.totals.split(',');
                    array.map(item => {
                        totalPrice += parseFloat(item);
                    });
                    totals.text(totalPrice.toFixed(2))
                    if(array.length == 1 && array[0] == '')
                        totals.text(0)
                },
                onUncheckAll: function(row,$element){
                    totals.text(0)
                },
                onCheckAll: function(row,$element){
                    let rows = $.table.selectColumns('total');
                    let data = { "totals": rows.join() };
                    let totalPrice = 0;
                    let array = data.totals.split(',');
                    array.map(item => {
                        totalPrice += parseFloat(item);
                    });

                    totals.text(totalPrice.toFixed(2))
                },
                columns: [{
                    checkbox: true,
                    formatter:function (value, row, index) {
                        if(row.status == 3 || row.status == 4) {
                            return { disabled: true};
                        }
                    }
                },
                {
                    field: 'cartId',
                    title: '购物车唯一标识',
                    visible: false
                },
                {
                    field: 'cosId',
                    title: '商品ID'
                },
                {
                    field: 'cosmetics.name',
                    title: '商品名称'
                },
                {
                    field: 'cosmetics.price',
                    title: '单价',
                    formatter: function (value, row, index) {
                        return '￥'+row.cosmetics.price
                    },
                },
                {
                    field: 'number',
                    title: '数量',
                    formatter: function (value, row, index) {
                        return row.number+' ('+row.cosmetics.unit+')'
                    },
                },
                {
                    field: '',
                    title: '总价',
                    formatter: function (value, row, index) {
                        return '￥'+row.total
                    },
                },
                {
                    field: '',
                    title: '状态',
                    formatter: function (value, row, index) {
                        if (row.status == 1) {
                            return '<strong style="color: #00a65a">正常</strong>';
                        } else if (row.status == 2) {
                            return '<strong style="color: #ec9f12">库存紧张</strong>';
                        } else if (row.status == 3) {
                            return '<strong style="color: #cb1711">库存不足</strong>';
                        } else if (row.status == 4) {
                            return '<strong style="color: #6e6f6f">商品已失效</strong>';
                        }
                    },
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.cartId + '\')" title="修改数量"><i class="fa fa-edit"></i></a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.cartId + '\')" title="删除记录"><i class="fa fa-remove"></i></a> ');
                        actions.push('<a class="btn btn-info btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="submitSingle(\'' + row.cartId + '\')" title="提交订单"><i class="fa fa-check"></i></a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        //单个下单
        function submitSingle(cartId) {
            $.modal.confirm("确认要提交选中的商品吗?", function() {
                var url = ctx + "admin/order/add"
                var data = { cartIds: cartId }
                mySubmit(url, "post", "json", data);
                });
        }

        //批量下单
        function submitAll() {
            table.set();
            var rows = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
            if (rows.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            $.modal.confirm("确认要提交选中的" + rows.length + "个商品记录吗?", function() {
                var url = ctx + "admin/order/add"
                var data = { "cartIds": rows.join() }
                mySubmit(url, "post", "json", data);
            });
        }

        //提交专属
        function mySubmit(url, type, dataType, data, callback) {
            var config = {
                url: url,
                type: type,
                dataType: dataType,
                data: data,
                beforeSend: function () {
                    $.modal.loading("正在处理中，请稍候...");
                },
                success: function(result) {
                    if (typeof callback == "function") {
                        callback(result);
                    }
                    $.operate.ajaxSuccess(result);
                    console.log(result.data["orderId"]);
                    $.modal.openTab("订单详情", ctx + "admin/order" + '/orderWithDetail/' + result.data["orderId"]);
                }
            };
            $.ajax(config)
        }

    </script>
</body>
</html>